package com.yami.shop.api.controller;

import com.yami.shop.bean.app.dto.CategoryDto;
import com.yami.shop.bean.model.Category;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.yami.shop.common.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author LGH
 */
@RestController
@RequestMapping("/category")
@Tag(name = "分类接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;



    @GetMapping("/categoryInfo")
    @Operation(summary =  "分类信息列表", description = "获取所有的产品分类信息，顶级分类的parentId为0,默认为顶级分类")
    @Parameters({
            @Parameter(name = "parentId", description = "分类ID"),
            @Parameter(name = "shopId", description = "店铺id")
    })
    public ServerResponseEntity<List<CategoryDto>> categoryInfo(@RequestParam(name="parentId", defaultValue = "0") Long parentId,
                                                          @RequestParam(name="shopId", defaultValue = "0") Long shopId) {
        List<Category> categories = categoryService.listByParentIdAndShopId(parentId, shopId);
        List<CategoryDto> categoryDtos = BeanUtil.mapAsList(categories, CategoryDto.class);
        return ServerResponseEntity.success(categoryDtos);
    }
}
